<template>
  <div class="lb-goods-list">
    <top-nav />
    <div class="page-main">
      <el-row class="page-top-operate">
        <el-button @click="getAuditList(-2)" plain size="medium"
          >全部（{{ countForm.all }}）</el-button
        >
        <el-button @click="getAuditList(0)" plain size="medium"
          >待处理（<span style="color: #f56c6c">{{ countForm.wait }}</span
          >）</el-button
        >
        <el-button @click="getAuditList(1)" plain size="medium"
          >审核通过（{{ countForm.adopt }}）</el-button
        >
        <el-button @click="getAuditList(-1)" plain size="medium"
          >驳回（{{ countForm.refuse }}）</el-button
        >
      </el-row>
      <el-row class="page-search-form">
        <el-form :inline="true" :model="searchForm" ref="searchForm">
          <el-form-item label="输入查询" prop="str">
            <el-input
              v-model="searchForm.str"
              placeholder="用户ID/手机号"
            ></el-input>
          </el-form-item>
          <el-form-item label="日期" prop="">
            <el-date-picker
              v-model="range"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="user_id" label="用户ID"> </el-table-column>
        <el-table-column prop="avatarUrl" label="头像">
          <template slot-scope="scope">
            <lb-image
              :src="
                scope.row.avatarUrl
                  ? scope.row.avatarUrl
                  : 'https://lbqnyv2.migugu.com/defaultAvatar.png'
              "
            />
          </template>
        </el-table-column>
        <el-table-column prop="nickName" show-overflow-tooltip label="昵称">
        </el-table-column>

        <el-table-column prop="true_name" label="姓名"> </el-table-column>
        <el-table-column prop="phone" label="手机号"> </el-table-column>
        <el-table-column prop="create_time" sortable label="创建时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                v-if="scope.row.status === 0"
                plain
                type="primary"
                >待审核</lb-button
              >
              <lb-button
                size="mini"
                v-if="scope.row.status === 1"
                plain
                type="success"
                >通过</lb-button
              >
              <lb-button
                size="mini"
                v-if="scope.row.status === -1"
                plain
                type="danger"
                >驳回</lb-button
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                @click="editMember(scope.row, false)"
                plain
                type="primary"
                >查看</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="primary"
                v-if="scope.row.status === 0"
                @click="editMember(scope.row, true)"
                >审核</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        :batch="false"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
    <lb-member
      @changeMember="changeMember"
      :showEdit="showEdit"
      :showMemberId="showMemberId"
      :showMember="showMember"
    ></lb-member>
  </div>
</template>

<script>
export default {
  data () {
    return {
      showMemberId: '',
      showEdit: false,
      showMember: false,
      range: [],
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > Date.now()
        }
      },
      countForm: {
        all: 0,
        refuse: 0,
        adopt: 0,
        wait: 0
      },
      loading: false,
      searchForm: {
        str: '',
        status: -2,
        level: '',
        page: 1,
        limit: 10
      },
      tableData: [],
      total: 0,
      multipleSelection: []
    }
  },
  created () {
    this.getAuditCount()
    this.getTableDataList()
  },
  activated () {
  },
  methods: {
    resetForm (name) {
      this.range = ''
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    resetTime () {
      this.searchForm.str = ''
      this.range = ''
    },
    getAuditList (status) {
      this.searchForm.status = status
      this.searchForm.page = 1
      this.getTableDataList()
    },
    changeMember () {
      this.getTableDataList()
    },
    editMember (item, isEdit) {
      this.showMemberId = item.id
      this.showMember = !this.showMember
      if (isEdit) {
        this.showEdit = true
      } else {
        this.showEdit = false
      }
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    // 获取申请数量
    getAuditCount () {
      this.$api.getDistributorCount().then(res => {
        console.log(res)
        this.countForm = res.data
      })
    },
    /**
     * @method 获取商品列表
     */
    getTableDataList () {
      this.loading = true
      let { searchForm, range } = this
      if (range && range.length > 0) {
        searchForm.start = range[0]
        searchForm.end = range[1]
      } else {
        searchForm.start = ''
        searchForm.end = ''
      }
      this.$api.getDistributorList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          console.log(res)
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-goods-list {
  width: 100%;
  .page-main {
    width: 100%;
    padding: 20px;

    .el-table {
      img {
        width: 80px;
        height: 80px;
      }
    }
  }
}
</style>
